package com.huluobo.a2204kotlinshopping.setting

import androidx.lifecycle.MutableLiveData
import com.huluobo.base.bean.CommonBeanResult
import com.huluobo.base.bean.UserBean
import com.huluobo.base.model.BaseRepo
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.MultipartBody.Part

/**
 *  Created by LC on 2024/8/6.
 */
class SettingRepo : BaseRepo() {
    fun updateIcon(part: Part, success: MutableLiveData<CommonBeanResult>, failed: MutableLiveData<String>) {
        apiService.updateIcon(part)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommonBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommonBeanResult) {
                    success.value = t
                }
            })
    }

    fun updateNickname(nickname: String, success: MutableLiveData<CommonBeanResult>, failed: MutableLiveData<String>) {
        apiService.updateNickname(nickname)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommonBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommonBeanResult) {
                    success.value = t
                }

            })
    }

    fun updateBirth(birth: String, success: MutableLiveData<CommonBeanResult>, failed: MutableLiveData<String>) {
        apiService.updateBirth(birth)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommonBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommonBeanResult) {
                    success.value = t
                }
            })
    }

    fun updateSex(sex: String, success: MutableLiveData<CommonBeanResult>, failed: MutableLiveData<String>) {
        apiService.updateSex(sex)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommonBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommonBeanResult) {
                    success.value = t
                }
            })
    }

    fun queryUserInfo(username: String, success: MutableLiveData<UserBean>, failed: MutableLiveData<String>) {
        db.userDao.queryUserInfo(username)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<UserBean> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: UserBean) {
                    success.value = t
                }
            })
    }
}